module.exports=class users_mod extends require('./model'){
  static LoginUser(id,password,type){
    type=Number (type);
    return new Promise((resolve,reject)=>{
      let sql="select * from user where binary id='"+id+"' and password='"+password+"' and type="+type;
      console.log(sql)
      this.query(sql).then((result)=>{
        resolve(result)
      }).catch(err=>{
        reject('登录失败')
      })
    })
  }

  static getUsersByTypePageMod(type,currPage,pageNum){
    pageNum=Number(pageNum);
    currPage=Number(currPage);
    currPage=Number(pageNum*currPage);
    return new Promise(((resolve, reject) => {
      let sql=`select * from user where type=`+type+` order by modifytime desc LIMIT ?,?`
      console.log(sql)
      this.query(sql,this.formatParams(currPage,pageNum)).then(result=>{
        resolve(result)
      }).catch(err=>{
        reject(err)
      })
    }))
  }

  static getUserByTypePageTotal(type){
    return new Promise((resolve,reject)=>{
      let sql="select count(1) as count from user where type="+type
      this.query(sql).then(result=>{
        resolve(result)
      }).catch(err=>{
        reject(err)
      })
    })
  }

  static delUserDataMod(id){
    return new Promise((resolve,reject)=>{
      let sql="delete from user where id="+id
      console.log(sql)
      this.query(sql).then(result=>{
        resolve('删除用户成功')
      }).catch(err=>{
        reject('删除用户失败')
      })
    })
  }

  // static delReadMod(id){
  //   return new Promise((resolve,reject)=>{
  //     let sql="delete from read where u_id="+id
  //     console.log(sql)
  //     this.query(sql).then(result=>{
  //       resolve('删除阅读表成功')
  //     }).catch(err=>{
  //       reject('删除阅读表用户失败')
  //     })
  //   })
  // }

  static upDataUserMod(u_id,username,sex,address,type,classes){
    return new Promise((resolve,reject)=>{
      let sql ="update user set username='"+username+"',sex='"+sex+"',address='"+address+"',type='"+type+"',classes='"+classes+"' where id="+u_id
     console.log(sql)
      this.query(sql).then(result=>{
        resolve('更新成功')
      }).catch(err=>{
        reject('更新失败')
      })
    })
  }

  /**获取所有用户
   *
   */
  static getAllUser(){
    return new Promise(((resolve, reject) => {
      let sql=`select * from user `
      console.log(sql)
      this.query(sql).then(result=>{
        resolve(result)
      }).catch(err=>{
        reject(err)
      })
    }))
  }

  static inXlsxData(inXlsxArr){
    return new Promise((resolve,reject)=>{
      for (let i=0;i<inXlsxArr.length;i++){
        let sql="insert into user (id,username,password,head,address,sex,classes,type) values("+inXlsxArr[i].id+",'"+inXlsxArr[i].username+"','"+inXlsxArr[i].password+"','"+inXlsxArr[i].head+"','"+inXlsxArr[i].address+"','"+inXlsxArr[i].sex+"','"+inXlsxArr[i].classes+"','"+inXlsxArr[i].type+"')"
        this.query(sql).then(result=>{
          resolve(result)
        }).catch(err=>{
          reject(err)
        })
      }
    })
  }


  /**
   * 修改个人用户密码
   * @param u_id
   * @param oldPassword
   * @param newPassword
   */
  static upPasswordMod(u_id,oldPassword,newPassword){
    return new Promise((resolve, reject) => {
      let sql="update `user` set password = ? where password =? and id=?"
      this.query(sql,this.formatParams(newPassword,oldPassword,u_id)).then(result=>{
        resolve(result)
      }).catch(err=>{
        reject(err)
      })
    })
  }
}
